var $, form, laypage, layer, table, laydate, tree, element, colorpicker;
var tableConfig = new Object();
var queryConfig = new Object();

!function() {
	$ = layui.jquery;
	window.jQuery=layui.$;
	form = layui.form;
	laypage = layui.laypage;
	layer = layui.layer;
	table = layui.table;
	laydate = layui.laydate;
	tree = layui.tree;
	element = layui.element;
	colorpicker = layui.colorpicker;
}();

tableConfig = {
	elem : '#dataTable',
	toolbar : '#dataTool',
	skin : 'line',
	even : true,
	method : 'post',
	contentType : 'application/json;charset=utf8',
	parseData : function(res) {
		return {
			"code" : "0",
			"msg" : res.messageInfo,
			"count" : res.pageUtil.pageRecord,
			"data" : res.resultList
		};
	},
	request : {
		pageName : 'pageIndex',
		limitName : 'pageSize'
	},
	limits : [ 20, 50, 100 ],
	page : {
		layout : [ 'count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip' ]
	},
	limit : 20,
	totalRow : false,
	title : '数据表格',
	text : {
		none : '暂无相关数据'
	}
}

queryConfig = {
	pageIndex : "1",
	pageSize : "99999999"
}

function json(path, param) {
	var result;
	$.ajax({
		type : "POST",
		url : path,
		cache : false,
		async : false,
		contentType : "application/json;charset=utf-8",
		data : JSON.stringify(param),
		dataType : "json",
		success : function(data) {
			result = data;
		}
	});
	return result
}

function jsonAsync(path, param, func){
	
	var asyncLayer=null;
	$.ajax({
		beforeSend:function(){
			asyncLayer=layer.msg('拉取数据中,请稍候......', { icon: 16, shade: 0.5,shadeClose:false,time:0 });
		},
		complete:function(){
			layer.close(asyncLayer);
		},
		type : "POST",
		url : path,
		cache : false,
		async : true,
		contentType : "application/json;charset=utf-8",
		data : JSON.stringify(param),
		dataType : "json", 
		success : function(data) {
			func(data);
		}
	});
}



function post(path, param) {
	var result;
	$.ajax({
		type : "POST",
		url : path,
		cache : false,
		async : false,
		data : param,
		dataType : "json",
		success : function(data) {
			result = data;
		}
	});
	return result
}

function postAsync(path, param, func){
	
	var postLayer=null;
	$.ajax({
		beforeSend:function(){
			postLayer=layer.msg('拉取数据中,请稍候......', { icon: 16, shade: 0.5,shadeClose:false,time:0 });
		},
		complete:function(){
			layer.close(postLayer);
		},
		type : "POST",
		url : path,
		cache : false,
		async : true,
		data : param,
		dataType : "json", 
		success : function(data) {
			func(data);
		}
	});
}

var StringUtil = {
	isEmpty: function(input) {
		return input == null || input == '';
	},
	isNotEmpty: function(input) {
		return !this.isEmpty(input);
	},
	isBlank: function(input) {
		return input == null || /^\s*$/.test(input);
	},
	isNotBlank: function(input) {
		return !this.isBlank(input);
	},
	trim: function(input) {
		return input.replace(/^\s+|\s+$/, '');
	},
	trimToEmpty: function(input) {
		return input == null ? "" : this.trim(input);
	},
	startsWith: function(input, prefix) {
		return input.indexOf(prefix) === 0;
	},
	endsWith: function(input, suffix) {
		var regex = new RegExp(this.escapeMetacharacterOfStr(suffix) + "$");
		return regex.test(input);
	},
	contains: function(input, searchSeq) {
		return input.indexOf(searchSeq) >= 0;
	},
	equals: function(input1, input2) {
		return input1 == input2;
	},
	equalsIgnoreCase: function(input1, input2) {
		return input1.toLocaleLowerCase() == input2.toLocaleLowerCase();
	},
	containsWhitespace: function(input) {
		return this.contains(input, ' ');
	},
	//生成指定个数的字符
	repeat: function(ch, repeatTimes) {
		var result = "";
		for(var i = 0; i < repeatTimes; i++) {
			result += ch;
		}
		return result;
	},
	deleteWhitespace: function(input) {
		return input.replace(/\s+/g, '');
	},
	rightPad: function(input, size, padStr) {
		return input + this.repeat(padStr, size);
	},
	leftPad: function(input, size, padStr) {
		return this.repeat(padStr, size) + input;
	},
	//首小写字母转大写
	capitalize: function(input) {
		var strLen = 0;
		if(input == null || (strLen = input.length) == 0) {
			return input;
		}
		return input.replace(/^[a-z]/, function(matchStr) {
			return matchStr.toLocaleUpperCase();
		});
	},
	//首大写字母转小写
	uncapitalize: function(input) {
		var strLen = 0;
		if(input == null || (strLen = input.length) == 0) {
			return input;
		}
		return input.replace(/^[A-Z]/, function(matchStr) {
			return matchStr.toLocaleLowerCase();
		});
	},
	//大写转小写，小写转大写
	swapCase: function(input) {
		return input.replace(/[a-z]/ig, function(matchStr) {
			if(matchStr >= 'A' && matchStr <= 'Z') {
				return matchStr.toLocaleLowerCase();
			} else if(matchStr >= 'a' && matchStr <= 'z') {
				return matchStr.toLocaleUpperCase();
			}
		});
	},
	//统计含有的子字符串的个数
	countMatches: function(input, sub) {
		if(this.isEmpty(input) || this.isEmpty(sub)) {
			return 0;
		}
		var count = 0;
		var index = 0;
		while((index = input.indexOf(sub, index)) != -1) {
			index += sub.length;
			count++;
		}
		return count;
	},
	//只包含字母
	isAlpha: function(input) {
		return /^[a-z]+$/i.test(input);
	},
	//只包含字母、空格
	isAlphaSpace: function(input) {
		return /^[a-z\s]*$/i.test(input);
	},
	//只包含字母、数字
	isAlphanumeric: function(input) {
		return /^[a-z0-9]+$/i.test(input);
	},
	//只包含字母、数字和空格
	isAlphanumericSpace: function(input) {
		return /^[a-z0-9\s]*$/i.test(input);
	},
	//数字
	isNumeric: function(input) {
		return /^(?:[1-9]\d*|0)(?:\.\d+)?$/.test(input);
	},
	//小数
	isDecimal: function(input) {
		return /^[-+]?(?:0|[1-9]\d*)\.\d+$/.test(input);
	},
	//负小数
	isNegativeDecimal: function(input) {
		return /^\-(?:0|[1-9]\d*)\.\d+$/.test(input);
	},
	//正小数
	isPositiveDecimal: function(input) {
		return /^\+?(?:0|[1-9]\d*)\.\d+$/.test(input);
	},
	//整数
	isInteger: function(input) {
		return /^[-+]?(?:0|[1-9]\d*)$/.test(input);
	},
	//正整数
	isPositiveInteger: function(input) {
		return /^\+?(?:0|[1-9]\d*)$/.test(input);
	},
	//负整数
	isNegativeInteger: function(input) {
		return /^\-(?:0|[1-9]\d*)$/.test(input);
	},
	//只包含数字和空格
	isNumericSpace: function(input) {
		return /^[\d\s]*$/.test(input);
	},
	isWhitespace: function(input) {
		return /^\s*$/.test(input);
	},
	isAllLowerCase: function(input) {
		return /^[a-z]+$/.test(input);
	},
	isAllUpperCase: function(input) {
		return /^[A-Z]+$/.test(input);
	},
	defaultString: function(input, defaultStr) {
		return input == null ? defaultStr : input;
	},
	defaultIfBlank: function(input, defaultStr) {
		return this.isBlank(input) ? defaultStr : input;
	},
	defaultIfEmpty: function(input, defaultStr) {
		return this.isEmpty(input) ? defaultStr : input;
	},
	//字符串反转
	reverse: function(input) {
		if(this.isBlank(input)) {
			input;
		}
		return input.split("").reverse().join("");
	},
	//只包含特殊字符、数字和字母（不包括空格，若想包括空格，改为[ -~]）
	isSpecialCharacterAlphanumeric: function(input) {
		return /^[!-~]+$/.test(input);
	},
	//中文校验
	isChinese: function(input) {
		return /^[\u4E00-\u9FA5]+$/.test(input);
	},
	//去掉中文字符
	removeChinese: function(input) {
		return input.replace(/[\u4E00-\u9FA5]+/gm, "");
	},
	//转义元字符
	escapeMetacharacter: function(input) {
		var metacharacter = "^$()*+.[]|\\-?{}";
		if(metacharacter.indexOf(input) >= 0) {
			input = "\\" + input;
		}
		return input;
	},
	//转义字符串中的元字符
	escapeMetacharacterOfStr: function(input) {
		return input.replace(/[-$^()*+.\[\]|\\?{}]/gm, "\\$&");
	},
	//中文转为unicode编码
	chineseToUnicode: function(input) {
		return input.replace(/[\u4E00-\u9FA5]/g, function(matchStr) {
			return "\\u" + matchStr.charCodeAt(0).toString(16);
		});
	}

};

Date.prototype.Format = function (fmt) { 
	var o = {
	    "M+": this.getMonth() + 1, // 月份
	    "d+": this.getDate(), // 日
	    "h+": this.getHours(), // 小时
	    "m+": this.getMinutes(), // 分
	    "s+": this.getSeconds(), // 秒
	    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
	    "S": this.getMilliseconds() // 毫秒
	};
	if (/(y+)/.test(fmt)) {
	    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o)
	    if (new RegExp("(" + k + ")").test(fmt)) 
	        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
}

function guid() {
    function S4() {
        return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    return (S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4()).toUpperCase();
}

function dateParser(str) {
	var strArray=str.split(" ");
	var strDate=strArray[0].split("-");
	var strTime=strArray[1].split(":");
	var res= new Date(strDate[0],(strDate[1]-parseInt(1)),strDate[2],strTime[0],strTime[1],strTime[2])
	return res;
}

function contain(obj,arr,key){
	if(!arr||arr.length<1){
		return false;
	}
	for(var i=0;i<arr.length;i++){
		var item=arr[i];
		if(obj==item){
			return true;
		}
		if(key&&obj==item[key]){
			return true;
		}
	}
	return false;
}

function dateDistance(min,max) {
	return dateParser(max).getTime()-dateParser(min).getTime();
}

function located(url) {
	window.location.href = url;
}

function trim(content) {
	return content.replace(/(^\s*)|(\s*$)/g, "");
}

function zero(num, n) {
    return (Array(n).join(0) + num).slice(-n);
}

function randomWord(range){
	  var str = "";
	  var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
	  for(var i=0; i<range; i++){
	    pos = Math.round(Math.random() * (arr.length-1));
	    str += arr[pos]; 
	  }
	  return str.toUpperCase();
}

function randomNumber(min,max){
	return Math.floor(Math.random()*(max-min+1)+min);
}

function parseUrl(url) {
    var a = document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':', ''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function () {
            var ret = {},
            seg = a.search.replace(/^\?/, '').split('&'),
            len = seg.length, i = 0, s;
            for (; i < len; i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;

        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
        hash: a.hash.replace('#', ''),
        path: a.pathname.replace(/^([^\/])/, '/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
        segments: a.pathname.replace(/^\//, '').split('/')
    };
}


function replaceParams(sourceUrl, newParams) {

    for (var x in newParams) {
        var hasInMyUrlParams = false;
        for (var y in sourceUrl.params) {
            if (x.toLowerCase() == y.toLowerCase()) {
                sourceUrl.params[y] = newParams[x];
                hasInMyUrlParams = true;
                break;
            }
        }
        //原来没有的参数则追加
        if (!hasInMyUrlParams) {
            sourceUrl.params[x] = newParams[x];
        }
    }
    var _result = sourceUrl.protocol + "://" + sourceUrl.host + ":" + sourceUrl.port + sourceUrl.path + "?";

    for (var p in sourceUrl.params) {
        _result += (p + "=" + sourceUrl.params[p] + "&");
    }

    if (_result.substr(_result.length - 1) == "&") {
        _result = _result.substr(0, _result.length - 1);
    }

    if (sourceUrl.hash != "") {
        _result += "#" + sourceUrl.hash;
    } 
    return _result;
}

function fullScreen() {
	var element = document.documentElement;
	if (element.requestFullscreen) {
		element.requestFullscreen();
	} else if (element.msRequestFullscreen) {
		element.msRequestFullscreen();
	} else if (element.mozRequestFullScreen) {
		element.mozRequestFullScreen();
	} else if (element.webkitRequestFullscreen) {
		element.webkitRequestFullscreen();
	}
	$("#exitScreen").css("display", "");
	$("#fullScreen").css("display", "none");
}

// 退出全屏
function exitScreen() {
	if (document.exitFullscreen) {
		document.exitFullscreen();
	} else if (document.msExitFullscreen) {
		document.msExitFullscreen();
	} else if (document.mozCancelFullScreen) {
		document.mozCancelFullScreen();
	} else if (document.webkitExitFullscreen) {
		document.webkitExitFullscreen();
	}
	$("#exitScreen").css("display", "none");
	$("#fullScreen").css("display", "");
}


function menuchange(menuId){
 	if(menuId=="mleft"){
 		menuclose();
 		localStorage.setItem("smenu", "mleft");
	}else{
		menuopen();
		localStorage.setItem("smenu", "mright");
	}
}
    
function menuopen(mode){
    $(".layui-nav-tree").removeClass("leftit");
    $("#mleft,.dark input").show();
    $("#mright").hide();
    $(".layui-nav-tree span,.layui-nav-tree h4").removeClass("disno");
    if(mode){
         $(".layui-nav-tree,.dark").css({"width":"200px" });
         $(".layui-body").css({"left":"205px"});
    }else{
         $(".layui-nav-tree,.dark").animate({"width":"200px" });
         $(".layui-body").animate({"left":"205px"});
    }
    $(".layui-body table").css({"width":"100%"});

}
function menuclose(mode) {
    $("#mright").show();
    $("#mleft,.dark input").hide();
    $(".layui-nav-tree").addClass("leftit");
    $(".layui-nav-tree span,.layui-nav-tree h4").addClass("disno");
    if(mode){
        $(".layui-nav-tree,.dark").css({"width":"50px" });
        $(".layui-body").css({"left":"55px"});
    }else{
         $(".layui-nav-tree,.dark").animate({"width":"50px"});
         $(".layui-body").animate({"left":"55px"});
    }
    $(".layui-body table").css({"width":"100%"});
}

//高级搜索
function moreSearch(idName){
	if($("#"+idName).css("display")=="none"){
		$("#"+idName).css("display","block");
	}else{
		$("#"+idName).css("display","none")
	};
}

